import 'package:dailoz/constants.dart';
import 'package:dailoz/entity/task_entity.dart';
import 'package:dailoz/widgets/day_task.dart';
import 'package:dailoz/widgets/task_label.dart';
import 'package:dailoz/widgets/task_widget.dart';
import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';

class TaskUtils {
  static Widget generateTaskList(String element, Map<String, dynamic> map,
      {Function? parentFunction}) {
    List<Widget> task = [];
    int status;
    String statusName;
    map[element].forEach((element) {
      TaskEntity taskEntity = TaskEntity.fromJson(element);
      Color? labelTextColor;
      Color? labelColor;
      Color? taskColor;
      Color? boxColor;
      status = taskEntity.status;
      switch (status) {
        case 1:
          statusName = "completed";
          labelTextColor = completedTaskLabelTextColor;
          labelColor = completedTaskLabelColor;
          taskColor = completedTaskColor;
          boxColor = completedTaskBoxColor;
          break;
        case 2:
          statusName = "cancel";
          labelTextColor = cancelTaskLabelTextColor;
          labelColor = cancelTaskLabelColor;
          taskColor = cancelTaskColor;
          boxColor = cancelTaskBoxColor;

          break;
        case 3:
          statusName = "pending";
          labelTextColor = pendingTaskLabelTextColor;
          labelColor = pendingTaskLabelColor;
          taskColor = pendingTaskColor;
          boxColor = pendingTaskBoxColor;

          break;
        case 4:
          statusName = "onGoing";
          labelTextColor = onGoingTaskLabelTextColor;
          labelColor = onGoingTaskLabelColor;
          taskColor = onGoingTaskColor;
          boxColor = onGoingTaskBoxColor;
          break;
      }
      List<Widget> tag = [];
      if (taskEntity.tag != null) {
        List<String>? tagList = taskEntity.tag!.split(",");
        for (var element in tagList) {
          tag.add(
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 3.0),
              child: TaskLabel(
                labelName: element,
                status: taskEntity.status,
              ),
            ),
          );
        }
      }
      task.add(
        TaskWidget(
          parentFunction: parentFunction,
          task: taskEntity,
          taskId: taskEntity.id!,
          margin: const EdgeInsets.only(right: 10.0),
          padding: const EdgeInsets.symmetric(horizontal: 15.0),
          height: 114.0,
          width: 190.0,
          taskName: taskEntity.taskContent,
          taskTime: '${formatDate(DateTime.parse(taskEntity.startTime), [
                HH,
                ':',
                nn
              ])} - ${formatDate(DateTime.parse(taskEntity.endTime), [
                HH,
                ':',
                nn
              ])}',
          labelGroupPadding: const EdgeInsets.only(
            bottom: 15.0,
          ),
          taskLabel: tag,
        ),
      );
    });
    return DayTask(taskWidgetList: task, time: element);
  }
}
